package MyGO.Form;

import MyGO.property.Medicine;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static MyGO.utils.DateTimeUtils.getTime.getDateTime;

@Data
public class MedicineForm {
    private String id;
    private Double fee;
    private List<String> medicines;
    private HashMap<String, Integer> medicineNum;
    private HashMap<String, String> medicineUsed;
    private String creatTime;
    private String finishTime;

    public MedicineForm() {
        this.fee = 0.0;
        medicines = new ArrayList<>();
        medicineNum = new HashMap<>();
        medicineUsed = new HashMap<>();
        creatTime = getDateTime();
    }

    public void addMedicine(Medicine medicine) {
        fee += medicine.getPrice();
        if (medicineNum.get(medicine.getId()) == null) {
            medicineNum.put(medicine.getId(), 1);
            medicines.add(medicine.getId());
        } else {
            medicineNum.put(medicine.getId(), medicineNum.get(medicine.getId()) + 1);
        }
    }

    public void deleteMedicine(Medicine medicine) {
        fee -= medicine.getPrice();
        if (medicineNum.get(medicine.getId()) == 1) {
            medicineNum.remove(medicine.getId());
            medicines.remove(medicine.getId());
            medicineUsed.get(medicine.getId());
        } else {
            medicineNum.put(medicine.getId(), medicineNum.get(medicine.getId()) - 1);
        }
    }

    public void countMedicine(Medicine medicine, int count) {
        int p_count = medicineNum.get(medicine.getId());
        medicineNum.put(medicine.getId(), count);
        fee += (count - p_count) * medicine.getPrice();
        if (count == 0) {
            medicineNum.remove(medicine.getId());
            medicineUsed.remove(medicine.getId());
            medicines.remove(medicine.getId());
        }
    }

    public void writeMadicineUsed(Medicine medicine, String used) {
        medicineUsed.put(medicine.getId(), used);
    }

    public void finish() {
        finishTime = getCreatTime();
    }
}
